﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using University.Workshop.Model;
using System.Globalization;

namespace University.Workshop.DataAccess
{
    public static class ClientSearcher
    {

        public static DataTable Search(string fullName, string id, string telephone)
        {
            Client client = new Client { FullName = fullName, ClientId = id, Telephone = telephone };
            return SearchClient(client); 
        }

        public static DataTable SearchClient(Client client)
        {
            if (client == null)
                throw new ArgumentException("Invalid Input Parameters");

            // La cadena de conexión
            const string conecctionString = "Data Source=.\\SQLEXPRESS; Initial Catalog=DataBaseHotel; Integrated Security=True";

            // Conectar y seleccionar
            using (SqlConnection conn = new SqlConnection(conecctionString))
            {
                SqlCommand cmdSelect;
 
                using (cmdSelect = new SqlCommand())
                {
                    cmdSelect.Parameters.Clear();

                    cmdSelect.CommandText = "SELECT * FROM [Client] WHERE [FullName] Like @FullName " + 
                        "OR  [ClientId] = @ClientId OR [Telephone] = @Telephone";

                    cmdSelect.CommandType = CommandType.Text;
                    cmdSelect.Connection = conn;

                    cmdSelect.Parameters.Add(new SqlParameter("@FullName", "%"+client.FullName+"%"));
                    cmdSelect.Parameters.Add(new SqlParameter("@ClientId", client.ClientId));
                    cmdSelect.Parameters.Add(new SqlParameter("@Telephone", client.Telephone));
                }

                SqlDataAdapter dab;
                
                using (DataTable dtb = new DataTable())
                {
                    dtb.Locale = CultureInfo.InvariantCulture;

                    using (dab = new SqlDataAdapter())
                    {
                        dab.SelectCommand = cmdSelect;
                        dab.Fill(dtb);
                    }
                    return dtb;
                }
            }
        }
    }
}
